import {
  Controller,
  Get,
  Render,
  Body,
  Post,
  UseInterceptors,
  UploadedFiles,
} from '@nestjs/common';
import { FilesInterceptor } from '@nestjs/platform-express';
import { createWriteStream } from 'fs';
import { join } from 'path';

@Controller('uploadmany')
export class UploadmanyController {
  @Get()
  @Render('default/uploadmany')
  index() {}

  @Post('doAdd')
  @UseInterceptors(FilesInterceptor('pic'))
  doAdd(@Body() body, @UploadedFiles() files) {
    // console.log(body);
    // console.log(file); // 文件将是一个数组
    // 如果要实现图片上传的话还需要进行一些判断
    for (const file of files) {
      const writeImage = createWriteStream(
        join(__dirname, '../../', 'public/upload', `${file.originalname}`),
      );
      writeImage.write(file.buffer);
    }
    return '文件上传成功';
  }
}
